
import os
import requests

from common.mylogger import logger
from common.myConf import MyConf
from common.my_path import conf_dir
from common.rsa_encrypt import generator_sign


class MyRequests:

    # 初始化方法
    def __init__(self):
        # 请求头
        self.headers = {
   'key': 'Content-Type',
   'value': 'application/',
   'type': 'text',
   'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
   'Content-Type': 'application/json',
   'Accept': '*/*',
   'Host': 'industry.handidit.com',
   'Connection': 'keep-alive'
}
        # 读取配置文件当中的，server地址。
        self.base_url = MyConf(os.path.join(conf_dir, "conf.ini")).get("server", "host")

    # 属性
    # 方法 post/put.. json=XXX , get..  params=XXX
    def send_requests(self,method,api_url, data,token=None):
        # 处理请求头
        self.__deal_header(token)
        # 处理请求url
        url = self.__deal_url(api_url)

        logger.info("请求url: \n{}".format(url))
        logger.info("请求方法: \n{}".format(method))

        # 如果是v3版本，则添加向请求体当中，添加timestamp和sign字段
        if self.headers.get("xxx-Type") == "xxx" and token:
            logger.info("使用RSA加密。")
            # 生成sign,和timestamp
            sign,timestamp = generator_sign(token)
            data["sign"] = sign
            data["timestamp"] = timestamp

        logger.info("请求数据: \n{}".format(data))

        # 调用requests的方法去发起一个请求。并得到响应结果
        if method.upper() == "GET":
            resp = requests.request(method, url, params=data, headers=self.headers)
        else:
            resp = requests.request(method, url, json=data, headers=self.headers)
        logger.info("响应结果：\n{}".format(resp.text))
        return resp

    def __deal_header(self,token=None):
        if token:
            self.headers["Authorization"] = "Bearer {}".format(token)
        logger.info("请求头为：\n{}".format(self.headers))

    def __deal_url(self,api_url:str):
        if api_url.startswith("https://") or api_url.startswith("http://"):
            return api_url
        else:
            url = self.base_url + api_url
            return url


if __name__ == '__main__':
    mr = MyRequests()
    url = ""
    req_data = {
        "mobile_phone": "18423504555",
        "pwd": "123456789",
        "reg_name": "zjt"
    }
    method = "post"
    resp = mr.send_requests(method,url,req_data)
    print(resp.json())

    # url地址
    url = ""
    # 请求类型：post

    # 请求体
    req_data = {
        "mobile_phone": "18610100020",
        "pwd": "123456789"
    }
    method = "post"
    resp = mr.send_requests(method,url,  req_data)
    print(resp.json())

    # 提取出来，给到下一接口去作为请求
    json_res = resp.json()
    token = json_res["data"]["token_info"]["token"]
    member_id = json_res["data"]["id"]

    url = ""
    # 请求数据
    req_data = {
        "member_id": member_id,
        "amount": 1000
    }
    method = "post"
    resp = mr.send_requests(method,url,  req_data,token=token)
    print(resp.json())